package rent.hj.launcher;

import android.app.Dialog;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by admin on 2017/12/18.
 */

public class LockService extends Service {
    private static final String TAG = "LockService";
    private MyBinder myBinder = new MyBinder();

    private Code mCode;
    private Time mTime;

    public static final String LOCK_COUNTDOWN_TIME_ACTION = "LOCK_COUNTDOWN_TIME_ACTION";
    public static final String TIME_EXTRA = "TIME_EXTRA";

    @Override
    public void onCreate() {
        super.onCreate();
        mCode = new Code(this);
        mTime = new Time(this);
    }

    private Timer timer = new Timer();

    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    public void showLockDialog() {
        final Dialog dialog = new CustomDialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
        dialog.getWindow().setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));

        dialog.setContentView(R.layout.lock_dialog);

        View viewById = dialog.findViewById(R.id.parent_layout);
        viewById.setClipToOutline(true);
        viewById.setOutlineProvider(new RoundViewOutlineProvider(10));

        ViewGroup keyboard_layout = (ViewGroup) dialog.findViewById(R.id.keyboard_layout);

        final String[] texts = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "X"};

        ViewGroup code_layout = (ViewGroup) dialog.findViewById(R.id.code_layout);

        final View error_txt = dialog.findViewById(R.id.error_txt);

        final TextView code1 = (TextView) code_layout.getChildAt(0);
        final TextView code2 = (TextView) code_layout.getChildAt(1);
        final TextView code3 = (TextView) code_layout.getChildAt(2);
        final TextView code4 = (TextView) code_layout.getChildAt(3);

        for (int i = 0; i < texts.length; i++) {
            final CustomButton customButton = new CustomButton(this);
            customButton.setText(texts[i]);
            customButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, 40);
            customButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!customButton.getText().toString().equals("X")) {
                        if (TextUtils.isEmpty(code1.getText())) {
                            code1.setText(customButton.getText());
                        } else if (TextUtils.isEmpty(code2.getText())) {
                            code2.setText(customButton.getText());
                        } else if (TextUtils.isEmpty(code3.getText())) {
                            code3.setText(customButton.getText());
                        } else if (TextUtils.isEmpty(code4.getText())) {
                            code4.setText(customButton.getText());
                            long time = checkValid(code1.getText().toString() + code2.getText() + code3.getText() + code4.getText());
                            if (time != -1){
                                overTime(time);
                                dialog.cancel();
                            }else {
                                error_txt.setVisibility(View.VISIBLE);
                            }
                        }
                    } else {
                        if (!TextUtils.isEmpty(code4.getText())) {
                            code4.setText("");
                        } else if (!TextUtils.isEmpty(code3.getText())) {
                            code3.setText("");
                        } else if (!TextUtils.isEmpty(code2.getText())) {
                            code2.setText("");
                        } else if (!TextUtils.isEmpty(code1.getText())) {
                            code1.setText("");
                        }
                    }
                }
            });
            keyboard_layout.addView(customButton);
        }

        dialog.show();
    }

    private long checkValid(String code) {
        return mCode.get(code);
    }

    //加钱
    private void overTime(final long time){
        Log.e(TAG, "overTime: "+(time == Code.TOW_HOURS?"两小时":time == Code.THREE_HOURS?"三小时":"验证码不对"));
        final long startTime = System.currentTimeMillis();
        mTime.setStartTime(startTime);
        mTime.setEndTime(startTime + time);
        timer.schedule(timerTask = new MyTimerTask(startTime + time),0,1000);
    }

    public class MyBinder extends Binder {

        public LockService getService() {
            return LockService.this;
        }
    }

    public void showReturnDialog() {
        final Dialog dialog = new CustomDialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
        dialog.getWindow().setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));

        dialog.setContentView(R.layout.default_dialog_layout);

        View viewById = dialog.findViewById(R.id.parent_layout);
        viewById.setClipToOutline(true);
        viewById.setOutlineProvider(new RoundViewOutlineProvider(10));

        View affirm = dialog.findViewById(R.id.affirm);
        View cancel = dialog.findViewById(R.id.cancel);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        affirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessage(0);
                timerTask.cancel();
                mTime.setEndTime(System.currentTimeMillis());
                dialog.dismiss();
            }
        });

        dialog.show();

    }

    private TimerTask timerTask;

    private class MyTimerTask extends TimerTask{

        private long endTime = -1;

        public MyTimerTask(long endTime) {
            this.endTime = endTime;
        }

        @Override
        public void run() {
            long time = System.currentTimeMillis();
            long lock_countdown_time = endTime - time;
            if (lock_countdown_time <= 0){
                cancel();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        showLockDialog();
                    }
                });
                return;
            }
            sendMessage(lock_countdown_time);
        }
    }

    private Handler handler = new Handler();

    private void sendMessage(long time){
        Intent intent = new Intent(LOCK_COUNTDOWN_TIME_ACTION);
        intent.putExtra(TIME_EXTRA,time / 1000);
        sendBroadcast(intent);
    }
}
